package com.fang.gateway.security.handler;

import com.fang.common.common.CommonConstants;
import com.fang.common.common.CommonResult;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.util.CommonResultUtil;
import com.fang.gateway.security.service.AuthorityService;
import com.fang.gateway.security.utils.HandlerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description 登录失败处理器
 * @Author Bernie
 * @Date 2024/4/23 10:45
 **/
@Slf4j
@Component
public class FluxAuthenticationFailureHandler implements ServerAuthenticationFailureHandler {

    @Resource
    private AuthorityService authorityService;

    @Override
    public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException e) {
        webFilterExchange.getExchange().getFormData().flatMap(map -> {
            Map<String, String> formParams = map.toSingleValueMap();
            String username = formParams.get(CommonConstants.CommonRequest.USERNAME);
            authorityService.addLoginRecord(null, username, CommonConstants.LoginStatus.FAIL, e.getMessage());
            return Mono.empty();
        });

        //返回json数据
        CommonResult result = null;
        if (e instanceof AccountExpiredException) {
            log.error("onAuthenticationFailure账号过期，AccountExpiredException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.USER_ACCOUNT_UNAVAILABLE);
        } else if (e instanceof BadCredentialsException) {
            log.error("onAuthenticationFailure密码错误，BadCredentialsException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.USER_PASSWORD_ERROR);
        } else if (e instanceof CredentialsExpiredException) {
            log.error("onAuthenticationFailure密码过期，CredentialsExpiredException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.USER_PASSWORD_EXPIRED);
        } else if (e instanceof DisabledException) {
            log.error("onAuthenticationFailure账号不可用，DisabledException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.USER_ACCOUNT_UNAVAILABLE);
        } else if (e instanceof LockedException) {
            log.error("onAuthenticationFailure账号锁定，LockedException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.ACCOUNT_LOCKED);
        } else if (e instanceof InternalAuthenticationServiceException) {
            log.error("onAuthenticationFailure账号不存在，InternalAuthenticationServiceException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.ACCOUNT_NOT_EXIST);
        } else if (e instanceof InsufficientAuthenticationException) {
            log.error("onAuthenticationFailure登录信息过期，InsufficientAuthenticationException===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.LOGIN_EXPIRED);
        } else {
            log.error("onAuthenticationFailure其他错误，e ===========>{}", e.getMessage());
            result = CommonResultUtil.failed(ExceptionCode.CommonCode.SYSTEM_BUSY);
        }
        return HandlerUtils.writeWith(webFilterExchange.getExchange(), result);
    }

}
